<?php
/**
 * 角色管理
 * @author zzdboy
 *
 */
require_once ROOT_DIR . 'Library' . DIRECTORY_SEPARATOR . 'Cms' . DIRECTORY_SEPARATOR . 'Tree.class.php';

class RoleAction extends AdminAction {
	
	public function index() {
		
		$AdminRole = D ( "AdminRole" );
		$role_list = $AdminRole->order ( "listorder ASC, roleid ASC" )->select ();
		
		$this->assign ( 'role_list', $role_list );
		$this->display ();
	}
	
	/**
	 * 添加角色
	 */
	public function add() {
		if ($this->isPost ()) {
			$AdminRole = D ( "AdminRole" );
			$data ['rolename'] = $this->_post ( "rolename" );
			if (empty ( $data ['rolename'] )) {
				$this->error ( "角色名称不能为空!" );
			}
			$data ['description'] = $this->_post ( "description" );
			if (empty ( $data ['description'] )) {
				$this->error ( "角色描述不能为空!" );
			}
			$data ['disabled'] = $this->_post ( "disabled" );
			$data ['listorder'] = $this->_post ( "listorder" );
			
			$AdminRole->create ( $data );
			$return = $AdminRole->add ();
			
			if ($return) {
				$this->success ( "添加角色成功!", "/role/" );
			} else {
				$this->error ( "添加角色失败!" );
			}
		} else {
			$this->display ();
		}
	}
	
	/**
	 * 编辑角色
	 */
	public function edit() {
		if ($this->isPost ()) {
			$AdminRole = D ( "AdminRole" );
			$data ['rolename'] = $this->_post ( "rolename" );
			if (empty ( $data ['rolename'] )) {
				$this->error ( "角色名称不能为空!" );
			}
			$data ['description'] = $this->_post ( "description" );
			if (empty ( $data ['description'] )) {
				$this->error ( "角色描述不能为空!" );
			}
			$data ['disabled'] = $this->_post ( "disabled" );
			$data ['listorder'] = $this->_post ( "listorder" );
			
			$roleid = $this->_post ( "roleid" );
			$return = $AdminRole->where ( "roleid={$roleid}" )->data ( $data )->save ();
			
			if ($return) {
				$this->success ( "编辑角色成功!", "/role/" );
			} else {
				$this->error ( "编辑角色失败!" );
			}
		} else {
			$roleid = $this->_get ( 'roleid' );
			
			$AdminRole = D ( "AdminRole" );
			$RoleInfo = $AdminRole->find ( $roleid );
			
			$this->assign ( 'RoleInfo', $RoleInfo );
			
			$this->display ();
		}
	}
	
	/**
	 * 删除角色
	 */
	public function delete() {
		$roleid = $this->_post ( 'roleid' );
		if (empty ( $roleid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数不能为空,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$AdminRole = D ( "AdminRole" );
		$return = $AdminRole->where ( "roleid={$roleid}" )->delete ();
		
		if ($return) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 权限设置
	 */
	public function priv_setting() {
		if ($this->isPost ()) {
		
		} else {
			$roleid = $this->_get ( 'roleid' );
			$this->assign ( 'roleid', $roleid );
			
			$AdminRole = D ( "AdminRole" );
			$role_list = $AdminRole->order ( "listorder ASC, roleid ASC" )->select ();
			
			$this->assign ( 'role_list', $role_list );
			
			$this->display ();
		}
	}
	
	public function rolepriv() {
		if ($this->isPost ()) {
			$roleid = $this->_post ( 'roleid' );
			$menuid = $_POST ['menuid'];
			
			$AdminRolePriv = D ( "AdminRolePriv" );
			$AdminRolePrivInfo = $AdminRolePriv->where ( "roleid={$roleid}" )->find ();
			if ($AdminRolePrivInfo) {
				$data ['data'] = serialize ( $menuid );
				$return = $AdminRolePriv->where ( "roleid={$roleid}" )->data ( $data )->save ();
			} else {
				$data ['roleid'] = $roleid;
				$data ['data'] = serialize ( $menuid );
				$AdminRolePriv->create ( $data );
				$return = $AdminRolePriv->add ();
			}
			if ($return) {
				$this->success ( "设置权限成功!", "/role/rolepriv/roleid/{$roleid}/" );
			} else {
				$this->error ( "设置权限失败!" );
			}
		} else {
			$roleid = $this->_get ( 'roleid' );
			$this->assign ( 'roleid', $roleid );
			
			$tree = new Cms_Tree ();
			$tree->icon = array (
					'│ ',
					'├─ ',
					'└─ ' 
			);
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			
			$Menu = D ( "Menu" );
			$result = $Menu->order ( "listorder ASC,id ASC" )->select ();
			
			$AdminRolePriv = D ( "AdminRolePriv" );
			$AdminRolePrivInfo = $AdminRolePriv->where ( "roleid={$roleid}" )->find ();
			
			if ($AdminRolePrivInfo) {
				$data = unserialize ( $AdminRolePrivInfo ['data'] );
			} else {
				$data = array ();
			}
			
			$AdminRole = D ( "AdminRole" );
			
			foreach ( $result as $n => $t ) {
				$result [$n] ['checked'] = (in_array ( $t ['id'], $data )) ? ' checked' : '';
				$result [$n] ['level'] = $AdminRole->get_level ( $t ['id'], $result );
				$result [$n] ['parentid_node'] = ($t ['parentid']) ? ' class="child-of-node-' . $t ['parentid'] . '"' : '';
			}
			$str = "<tr id='node-\$id' \$parentid_node>
			<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
			</tr>";
			
			$tree->init ( $result );
			$categorys = $tree->get_tree ( 0, $str );
			
			$this->assign ( 'categorys', $categorys );
			
			$this->display ();
		}
	}
	
	/**
	 * 栏目权限
	 */
	public function setting_cat_priv() {
		if ($this->isPost ()) {
		
		} else {
			$roleid = $this->_get ( 'roleid' );
			$this->assign ( 'roleid', $roleid );
			
			$AdminRole = D ( "AdminRole" );
			$role_list = $AdminRole->order ( "listorder ASC, roleid ASC" )->select ();
			
			$this->assign ( 'role_list', $role_list );
			
			$this->display ();
		}
	}
	
	public function columnpriv() {
		if ($this->isPost ()) {
		
		} else {
			$roleid = $this->_get ( 'roleid' );
			$this->assign ( 'roleid', $roleid );
			
			$this->display ();
		}
	}
	
	/**
	 * 成员管理
	 */
	public function member() {
		
		$roleid = $this->_get ( 'roleid' );
		
		$Admin = D ( "Admin" );
		
		// 导入分页类
		import ( "ORG.Util.Page" );
		$count = $Admin->where ( "roleid={$roleid}" )->count ();
		$Page = new Page ( $count, 100 );
		$show = $Page->show ();
		$list = $Admin->where ( "roleid={$roleid}" )->order ( "uid DESC" )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		if ($list) {
			foreach ( $list as &$val ) {
				$AdminRole = D ( "AdminRole" );
				$Role_info = $AdminRole->find ( $val ['roleid'] );
				$val ['rolename'] = $Role_info ['rolename'];
				$val ['lastlogintime'] = date ( "Y-m-d H:i:s", $val ['lastlogintime'] );
			}
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'page', $show );
		$this->display ();
	}
	
	/**
	 * 设置状态
	 */
	public function setStatus() {
		$disabled = $this->_post ( 'disabled' );
		$roleid = $this->_post ( 'roleid' );
		
		if (! isset ( $disabled ) || ! is_numeric ( $disabled ) || empty ( $roleid ) || ! is_numeric ( $roleid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$AdminRole = D ( "AdminRole" );
		
		$data ['disabled'] = $disabled;
		$return = $AdminRole->where ( "roleid={$roleid}" )->data ( $data )->save ();
		
		if ($return) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '设置成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,设置失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>